//
//  WorkOrderCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import SnapKit

class WorkOrderCell: UITableViewCell {
  static func height(with model: WorkOrder) -> CGFloat {
    return 95 + UIFont.calculateHeight(width: UIScreen.width - 56, font: GlobalConfig.system16Font, text: model.title)
  }
  
  static var identifier: String { return "WorkOrderCell.swxc" }
  
  var model: WorkOrder? {
    didSet {
      guard let temp = model else { return }
      
      titleLabel.text = temp.title
      dateLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.createTime)
      stateChange(with: temp)
    }

  }
  
  fileprivate var containerV: UIView!
  fileprivate var stateLabel: UILabel!
  fileprivate var stateV: UIView!
  fileprivate var titleLabel: UILabel!
  fileprivate var dateLabel: UILabel!
  fileprivate var finishStateV: UIImageView!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension WorkOrderCell {
  
  fileprivate func priorityLevel(with order: WorkOrder) {
    stateV.isHidden = true
    
    if "NORMAL" == order.priorityLevel {
      stateLabel.text = "一般"
      stateLabel.textColor = GlobalConfig.PaleTealColor
    }
    else if "URGENT" == order.priorityLevel{
      stateLabel.text = "紧急"
      stateLabel.textColor = GlobalConfig.GrapeFruitColor
    }
    else if "VERY_URGENT" == order.priorityLevel {
      stateLabel.text = "非常紧急"
      stateLabel.textColor = GlobalConfig.GrapeFruitColor
    }
    
    if "FINISHED" == order.status {
      stateLabel.textColor = GlobalConfig.TextLightColor
    } else {
      titleLabel.textColor = GlobalConfig.TextDeepColor
    }
    
  }
  
  fileprivate func stateChange(with order: WorkOrder) {
    
    finishStateV.isHidden = true
    
    // 超时且没完结
    if (order.timeOut as! Bool) && "FINISHED" != order.status {
        titleLabel.textColor = GlobalConfig.TextDeepColor
      stateV.isHidden = false
      stateV.backgroundColor = GlobalConfig.GrapeFruitColor
      
      stateLabel.text = "超时工单"
      stateLabel.textColor = GlobalConfig.GrapeFruitColor
      return
    }
      // 完结
    else if "FINISHED" == order.status {
      titleLabel.textColor = GlobalConfig.TextLightColor
      priorityLevel(with: order)
      finishStateV.isHidden = false
    }
      // 受理中的
    else if "ACCEPTED" == order.status {
        titleLabel.textColor = GlobalConfig.TextDeepColor
      stateV.isHidden = false
      stateV.backgroundColor = GlobalConfig.BGSkyBlueColor
      
      priorityLevel(with: order)
    }
      // 未接工单
    else if "UNTREATED" == order.status {
        titleLabel.textColor = GlobalConfig.TextDeepColor
      stateV.isHidden = false
      stateV.backgroundColor = GlobalConfig.BGSkyBlueColor
      
      stateLabel.text = "待接工单"
      stateLabel.textColor = GlobalConfig.BGSkyBlueColor
    }
    
  }
  
  fileprivate func create() {
    containerV = UIView()
    contentView.addSubview(containerV)
    containerV.backgroundColor = UIColor.white
    containerV.snp.makeConstraints { (make) in
      make.left.equalTo(self).offset(10)
      make.right.equalTo(self).offset(-10)
      make.top.equalTo(self).offset(2.5)
      make.bottom.equalTo(self).offset(-2.5)
    }
    containerV.layer.cornerRadius = 3
    containerV.clipsToBounds = true
    
    stateLabel = UILabel()
    containerV.addSubview(stateLabel)
    stateLabel.font = GlobalConfig.system15Font
    stateLabel.snp.makeConstraints { (make) in
      make.top.equalTo(containerV).offset(18)
      make.left.equalTo(containerV).offset(18)
      make.right.equalTo(containerV).offset(-18)
    }

    stateV = UIView()
    containerV.addSubview(stateV)
    stateV.snp.makeConstraints { (make) in
      make.height.equalTo(20)
      make.width.equalTo(3)
      make.left.equalToSuperview()
      make.centerY.equalTo(stateLabel)
    }

    titleLabel = UILabel()
    containerV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.numberOfLines = 0
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(containerV).offset(18)
      make.right.equalTo(containerV).offset(-18)
      make.top.equalTo(stateLabel.snp.bottom).offset(16)
    }

    dateLabel = UILabel()
    containerV.addSubview(dateLabel)
    dateLabel.font = GlobalConfig.system12Font
    dateLabel.textColor = GlobalConfig.TextLightColor
    dateLabel.snp.makeConstraints { (make) in
      make.left.equalTo(containerV).offset(18)
      make.right.equalTo(containerV).offset(-18)
      make.top.equalTo(titleLabel.snp.bottom).offset(10)
    }
    
    finishStateV = UIImageView()
    finishStateV.image = UIImage(named: "show-list-icon-grey")
    containerV.addSubview(finishStateV)
    finishStateV.snp.makeConstraints { (make) in
      make.top.right.equalToSuperview()
      make.width.height.equalTo(30)
    }
    finishStateV.isHidden = true
    
    
  }
}
